/*
  S.M.A.C.K - An operating system kernel
  Copyright (C) 2010,2011 Mattias Holm and Kristian Rietveld
  For licensing and a full list of authors of the kernel, see the files
  COPYING and AUTHORS.
*/

#include <stdint.h>
#include <stddef.h>
#include <vm.h>
#include <omap-gpio.h>

/* FIXME: This driver needs a clean up and made more generic.
 * You want to have API like: omap_gpio_set (int gpio_num, int enable).
 */

struct gpio
{
  uint32_t padding[36];
  uint32_t gpio_cleardataout;
  uint32_t gpio_setdataout;
};

static volatile struct gpio *gpio4;
static volatile struct gpio *gpio6;

/* NOTE: The following calls use BeagleBoard-specific GPIOs */
void
omap_gpio_set_usr0_led(int enable)
{
  if (enable)
    gpio4->gpio_setdataout = 1 << 22;
  else
    gpio4->gpio_cleardataout = 1 << 22;
}

void
omap_gpio_set_usr1_led(int enable)
{
  if (enable)
    gpio4->gpio_setdataout = 1 << 21;
  else
    gpio4->gpio_cleardataout = 1 << 21;
}

void
omap_gpio_set_dvid(int enable)
{
  /* Set pin GPIO 170 to control the DVI-D interface. */
  if (enable)
    gpio6->gpio_setdataout = 0x400;
  else
    gpio6->gpio_cleardataout = 0x400;
}

void
omap_gpio_init(void)
{
  gpio4 = vm_map_physical(vm_get_kernel_map(VM_REG_DEVICE), VM_SUPER_RW | VM_DEVICE,
                          (uintptr_t)0x49056000, 4096);

  gpio6 = vm_map_physical(vm_get_kernel_map(VM_REG_DEVICE), VM_SUPER_RW | VM_DEVICE,
                          (uintptr_t)0x49058000, 4096);

  /* FIXME: temporary as test: */
  omap_gpio_set_usr0_led(0);
}
